package com.lanlian.cayman.fragment;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.TextView;

import com.lanlian.cayman.R;
import com.lanlian.cayman.activity.RiskStateActivity;
import com.lanlian.commonlib.base.BaseActivity;
import com.lanlian.commonlib.base.BaseFragment;
import com.lanlian.uikit.MyGridView;
import com.lanlian.uikit.MyListView;
import com.lanlian.uikit.ResearchSelectAdapter;
import com.lanlian.utillib.MaxTextLengthFilter;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * <pre>
 *     author : huxiaobo
 *     e-mail : huxiaobo@xlanlian.com
 *     date   : 2018/11/13 15:49
 *     desc   : todo
 *     version: 1.0
 * </pre>
 */
@SuppressLint("ValidFragment")
public class RiskResearchThreeFragment extends BaseFragment implements View.OnClickListener {

    private GridView mGridView;
    private TextView mTvNext;
    private TextView mTvPrevious;
    private MyGridView mGridView2;
    private MyGridView mGridView3;
    private MyGridView mGridView4;
    private MyGridView mGridView5;
    private MyGridView mGridView6;
    private EditText mEt;
    private MyListView mGridView8;

    /**
     * 问题
     */
    ArrayList<String> quest = new ArrayList<>();

    private OnNextClickListener mOnNextClickListener;

    public interface OnNextClickListener {
        void onNextClick();

        void onPreviousClick();
    }

    public RiskResearchThreeFragment(OnNextClickListener onNextClickListener) {
        this.mOnNextClickListener = onNextClickListener;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_research_three, null);
        initView(view);
        initData();
        setListeners();
        setAdapter();
        return view;
    }

    @Override
    protected void initData() {
        quest = new ArrayList<>();
        quest.add("1、是否持有港股、海外股票？");
        quest.add("2、是否持有期货、期权、杠杆式外汇交易合约？");
        quest.add("3、是否持有存款、债券、债券基金或简单的保本产品？");
        quest.add("4、是否持有单位信托基金、互惠基金？");
        quest.add("5、是否持有股票挂钩存款/票据、货币挂钩存款、其他结构性存款/产品？");
        quest.add("6、是否持有对冲基金、非上市（场外）衍生品？");
        quest.add("7、持有的其他投资产品");
        quest.add("8、以下哪句话最能准确描述您在固定收益证券、结构性/衍生产品、期货、期权、认股权证或对冲基金的投资经验？");
    }

    @Override
    protected void initView(View view) {
        super.initView(view);
        mGridView = view.findViewById(R.id.grid_view);
        mTvNext = view.findViewById(R.id.tv_next);
        mTvPrevious = view.findViewById(R.id.tv_previous);
        mGridView2 = view.findViewById(R.id.grid_view2);
        mGridView3 = view.findViewById(R.id.grid_view3);
        mGridView4 = view.findViewById(R.id.grid_view4);
        mGridView5 = view.findViewById(R.id.grid_view5);
        mGridView6 = view.findViewById(R.id.grid_view6);
        mEt = view.findViewById(R.id.et);
        mGridView8 = view.findViewById(R.id.grid_view8);
        mEt.setFilters(new InputFilter[]{new MaxTextLengthFilter(150, getActivity())});
    }

    @Override
    protected void setListeners() {
        super.setListeners();
        mTvNext.setOnClickListener(this);
        mTvPrevious.setOnClickListener(this);
    }

    public ArrayList<JSONObject> getData() {
        ArrayList<JSONObject> list = null;
        try {
            list = new ArrayList<>();
            JSONObject jo = new JSONObject();
            jo.put("topicId", "11");
            jo.put("topicAnswerId", mGridView.getCheckedItemPosition() + 47 + "");
            jo.put("topicTitle", quest.get(0).substring(2));
            jo.put("topicAnswerDesc", list_mudi.get(mGridView.getCheckedItemPosition()));
            jo.put("supplementText", "");

            JSONObject jo2 = new JSONObject();
            jo2.put("topicId", "12");
            jo2.put("topicAnswerId", mGridView2.getCheckedItemPosition() + 49 + "");
            jo2.put("topicTitle", quest.get(1).substring(2));
            jo2.put("topicAnswerDesc", list_mudi.get(mGridView2.getCheckedItemPosition()));
            jo2.put("supplementText", "");

            JSONObject jo3 = new JSONObject();
            jo3.put("topicId", "13");
            jo3.put("topicAnswerId", mGridView3.getCheckedItemPosition() + 51 + "");
            jo3.put("topicTitle", quest.get(2).substring(2));
            jo3.put("topicAnswerDesc", list_mudi.get(mGridView3.getCheckedItemPosition()));
            jo3.put("supplementText", "");

            JSONObject jo4 = new JSONObject();
            jo4.put("topicId", "14");
            jo4.put("topicAnswerId", mGridView4.getCheckedItemPosition() + 53 + "");
            jo4.put("topicTitle", quest.get(3).substring(2));
            jo4.put("topicAnswerDesc", list_mudi.get(mGridView4.getCheckedItemPosition()));
            jo4.put("supplementText", "");

            JSONObject jo5 = new JSONObject();
            jo5.put("topicId", "15");
            jo5.put("topicAnswerId", mGridView5.getCheckedItemPosition() + 55 + "");
            jo5.put("topicTitle", quest.get(4).substring(2));
            jo5.put("topicAnswerDesc", list_mudi.get(mGridView5.getCheckedItemPosition()));
            jo5.put("supplementText", "");

            JSONObject jo6 = new JSONObject();
            jo6.put("topicId", "16");
            jo6.put("topicAnswerId", mGridView6.getCheckedItemPosition() + 57 + "");
            jo6.put("topicTitle", quest.get(5).substring(2));
            jo6.put("topicAnswerDesc", list_mudi.get(mGridView6.getCheckedItemPosition()));
            jo6.put("supplementText", "");

            JSONObject jo7 = new JSONObject();
            jo7.put("topicId", "17");

            jo7.put("topicTitle", quest.get(6).substring(2));

            jo7.put("supplementText", mEt.getText().toString());

            if (mEt.getText().toString().equals("")) {
                jo7.put("topicAnswerId", 60 + "");
                jo7.put("topicAnswerDesc", "否");
            } else {
                jo7.put("topicAnswerDesc", "");
                jo7.put("topicAnswerId", 59 + "");
            }

            JSONObject jo8 = new JSONObject();
            jo8.put("topicId", "18");
            jo8.put("topicAnswerId", mGridView8.getCheckedItemPosition() + 61 + "");
            jo8.put("topicTitle", quest.get(7).substring(2));
            jo8.put("topicAnswerDesc", list8.get(mGridView8.getCheckedItemPosition()));
            jo8.put("supplementText", "");
            list.add(jo);
            list.add(jo2);
            list.add(jo3);
            list.add(jo4);
            list.add(jo5);
            list.add(jo6);
            list.add(jo7);
            list.add(jo8);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return list;
    }

    ArrayList<String> list_mudi;
    ArrayList<String> list8;

    private void setAdapter() {
        list_mudi = new ArrayList<>();
        list_mudi.add("是");
        list_mudi.add("否");
        mGridView.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE);
        mGridView.setAdapter(new ResearchSelectAdapter(getActivity(), list_mudi));

        mGridView2.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE);
        mGridView2.setAdapter(new ResearchSelectAdapter(getActivity(), list_mudi));

        mGridView3.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE);
        mGridView3.setAdapter(new ResearchSelectAdapter(getActivity(), list_mudi));

        mGridView4.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE);
        mGridView4.setAdapter(new ResearchSelectAdapter(getActivity(), list_mudi));

        mGridView5.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE);
        mGridView5.setAdapter(new ResearchSelectAdapter(getActivity(), list_mudi));

        mGridView6.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE);
        mGridView6.setAdapter(new ResearchSelectAdapter(getActivity(), list_mudi));

        list8 = new ArrayList<>();
        list8.add("毫无经验");
        list8.add("经验有限，有一些投资经验，但需要详细指点");
        list8.add("有经验，有一些投资经验，可在略微指点下自行做出投资决定");
        list8.add("经验丰富，有丰富经验的积极投资者，更愿意自己做出投资决定");

        mGridView8.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE);
        mGridView8.setAdapter(new ResearchSelectAdapter(getActivity(), list8));
    }

    /**
     * 返回null 全部填写 否则返回错误提示
     *
     * @return
     */
    private String checkSelect() {
        String s1 = "您未回答问题:";
        if (mGridView.getCheckedItemPosition() == -1) {
            return s1 + quest.get(0);
        } else if (mGridView2.getCheckedItemPosition() == -1) {
            return s1 + quest.get(1);
        } else if (mGridView3.getCheckedItemPosition() == -1) {
            return s1 + quest.get(2);
        } else if (mGridView4.getCheckedItemPosition() == -1) {
            return s1 + quest.get(3);
        } else if (mGridView5.getCheckedItemPosition() == -1) {
            return s1 + quest.get(4);
        } else if (mGridView6.getCheckedItemPosition() == -1) {
            return s1 + quest.get(5);
        } else if (mGridView8.getCheckedItemPosition() == -1) {
            return s1 + quest.get(7);
        }
        return null;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_next:
                RiskStateActivity fa = (RiskStateActivity) getActivity();
                String msg = checkSelect();
                if (msg != null) {
                    fa.showToast(msg);
                    return;
                }
                mOnNextClickListener.onNextClick();
                break;
            case R.id.tv_previous:
                mOnNextClickListener.onPreviousClick();
                break;
            default:
                break;
        }
    }

}
